package online.longzipeng.cinemasys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.entity.Hall;
import online.longzipeng.cinemasys.entity.Seat;
import online.longzipeng.cinemasys.mapper.HallMapper;
import online.longzipeng.cinemasys.mapper.SeatMapper;
import online.longzipeng.cinemasys.service.HallService;
import online.longzipeng.cinemasys.vo.hall.HallQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-09
 */
@Service
public class HallServiceImpl extends ServiceImpl<HallMapper, Hall> implements HallService {

    @Autowired
    HallMapper hallMapper;
    @Autowired
    SeatMapper seatMapper;

    @Override
    @Transactional
    public void add(Hall hall) {
        // 先验证名称是否重复，再保存影厅
        QueryWrapper<Hall> qwHall = new QueryWrapper<>();
        qwHall.eq("name",hall.getName());
        Hall hallQ = getOne(qwHall);
        if (hallQ != null) {
            throw new AppException(ResponseEnum.EXCEPTION_NAME_CONFIRMATION);
        }
        // 保存影厅后通过名称查询影厅，
        save(hall);
        qwHall = new QueryWrapper<>();
        qwHall.eq("name",hall.getName());
        hallQ = null;
        hallQ = getOne(qwHall);
        // 自动生成该影厅的座位
        if (hallQ != null) {
            // 先自动生成座位，再保存影厅
            int i = 1;
            int j = 1;
            int seats = (int)hall.getSeats();
            while (seats>0) {
                // 生成座位，每排12个
                for (;j<=12;j++) {
                    Seat seat = new Seat();
                    seat.setName(i+"排"+j+"座");
                    seat.setHallId(hallQ.getId());
                    seatMapper.insert(seat);
                    seats--;
                }
                i++;
                j=1;
                if (j>12) {
                    j=1;
                }
            }
        }else {
            throw new AppException(ResponseEnum.EXCEPTION_SEAT_CREATE_FAILED);
        }
    }

    @Override
    public void updateHall(Hall hall) {
        updateById(hall);
    }

    @Override
    public IPage<Hall> getListPage(HallQueryVO queryVO) {
        Page<Hall> hallPage = new Page<>(queryVO.getPage(),queryVO.getSize());
        QueryWrapper<Hall> qw = new QueryWrapper<>();
        // 先判断是交集还是并集 再判断查询条件 名称 描述
        if (queryVO.getSearchType()) {
            qw.like(!StringUtils.isEmpty(queryVO.getName()),"name",queryVO.getName());
            qw.like(!StringUtils.isEmpty(queryVO.getDescription()),"description",queryVO.getDescription());
        }else {
            qw.or().like(!StringUtils.isEmpty(queryVO.getName()),"name",queryVO.getName());
            qw.or().like(!StringUtils.isEmpty(queryVO.getDescription()),"description",queryVO.getName());
        }
        // 判断排序
        if ("desc".equals(queryVO.getSort())) {
            qw.orderByDesc("id");
        }else {
            qw.orderByAsc("id");
        }
        return page(hallPage,qw);
    }

    @Override
    public Object findById(Long id) {
        return getById(id);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        // 先删除相关座位，再删除影厅
        QueryWrapper<Seat> qw = new QueryWrapper<>();
        qw.eq("hall_id",id);
        seatMapper.delete(qw);
        removeById(id);
    }
}
